package com.erp.module.pieceworkGrouping.PieceworkChaxun

import android.util.Log
import android.view.View
import androidx.appcompat.app.AlertDialog
import com.erp.R
import com.erp.activity.BaseActivity
import com.erp.bean.PieceworkcollBean
import com.erp.module.pieceworkGrouping.PieceworkGroupingContract
import com.erp.myAdapter.PiecewrokChaxunAdapter
import com.erp.util.Dialogutil
import com.erp.util.Myutil
import com.erp.view.PullToRefreshView
import kotlinx.android.synthetic.main.activity_piecework_chaxun.*
import kotlinx.android.synthetic.main.header_knoya.iv_header_left
import kotlinx.android.synthetic.main.header_knoya.tv_header_title

class PieceworkChaxunActivity : BaseActivity(), View.OnClickListener,PieceworkGroupingContract.ProductionChaxunView, PullToRefreshView.OnHeaderRefreshListener, PullToRefreshView.OnFooterRefreshListener {
    lateinit var PieceworkList : MutableList<PieceworkcollBean>
    lateinit var pgadapter : PiecewrokChaxunAdapter
    lateinit var P : PieceworkGroupingContract.ProductionChaxunKP
    var code = 1
    var updialog: AlertDialog?=null
    override fun getLayoutId(): Int {
        return R.layout.activity_piecework_chaxun
    }

    override fun initCreate() {
        PieceworkList = ArrayList()
        pgadapter = PiecewrokChaxunAdapter(PieceworkList,this)
        LvPieceworkChaxunList.adapter = pgadapter
        P = PieceworkChaxunActivityP(this,this,PieceworkList)
        BtMingxi.setOnClickListener(this)
        BtRiqiHuizong.setOnClickListener(this)
        BtYuefenHuizong.setOnClickListener(this)
        Pull.setOnHeaderRefreshListener(this)
        Pull.setOnFooterRefreshListener(this)
        Pull.setheader(true)
        Pull.headerRefreshing()
        tv_header_title.text = "查询"
        iv_header_left.setOnClickListener(this)
        iv_header_left.visibility = View.VISIBLE
        TvStartTime.setOnClickListener(this)
        TvEndTime.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when(v){
            BtMingxi -> {
                code = 1
                pgadapter.setCode(code)
                updialog = Myutil.showupdatedialog(this, "正在查询", false)
                P.setCode(code,TvStartTime.text.toString(),TvEndTime.text.toString(),etBatchName.text.toString())
                P.xiala()
            }
            BtRiqiHuizong -> {
                code = 2
                pgadapter.setCode(code)
                updialog = Myutil.showupdatedialog(this, "正在查询", false)
                P.setCode(code,TvStartTime.text.toString(),TvEndTime.text.toString(),etBatchName.text.toString())
                P.xiala()
            }
            BtYuefenHuizong -> {
                code = 3
                pgadapter.setCode(code)
                updialog = Myutil.showupdatedialog(this, "正在查询", false)
                P.setCode(code,TvStartTime.text.toString(),TvEndTime.text.toString(),etBatchName.text.toString())
                P.xiala()
            }
            iv_header_left -> {
                finish()
            }
            TvStartTime -> {
                Dialogutil.showBottoPopupWindow(TvStartTime, "选择开始时间", TvStartTime.text.toString(), this)
                Myutil.notjianpan(this, iv_header_left)
            }
            TvEndTime -> {
                Dialogutil.showBottoPopupWindow(TvEndTime, "选择开始时间", TvEndTime.text.toString(), this)
                Myutil.notjianpan(this, iv_header_left)
            }
        }
    }

    override fun onFooterRefresh(view: PullToRefreshView?) {
        P.shangla()
    }

    override fun onHeaderRefresh(view: PullToRefreshView?) {
        P.xiala()
    }

    override fun xialaOk() {
        Pull.onHeaderRefreshComplete()
        pgadapter.notifyDataSetChanged()
        updialog?.dismiss()
    }

    override fun xialaNo() {
        Pull.onHeaderRefreshComplete()
        updialog?.dismiss()
    }

    override fun shanglaOk() {
        Pull.onFooterRefreshComplete()
        pgadapter.notifyDataSetChanged()
        updialog?.dismiss()
    }

    override fun shanglaNo() {
        Pull.onFooterRefreshComplete()
        updialog?.dismiss()
    }

    override fun gainDataOk() {
        pgadapter.notifyDataSetChanged()
    }


}